package com.yy.interceptor;

import base.CommonConstans;
import base.CurrentUserUtils;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import utils.CurrentLoginUser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: lyy
 * Date: 2021/12/13 16:03
 */
@Component
public class GatewayTokenInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String userInfoStr = request.getHeader(CommonConstans.CLIENT_HEAD);
        CurrentLoginUser currentLoginUser = JSONObject.parseObject(userInfoStr, CurrentLoginUser.class);
        CurrentUserUtils.setCurrentUser(currentLoginUser);
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        CurrentUserUtils.remove();
    }

}
